package com.pg.agent.engine.client.vo;

import cn.hutool.core.util.ObjectUtil;
import com.pg.agent.engine.client.model.InParam;
import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数对象
 */
@Data
public class HttpParam {
    /**
     * header参数
     */
    private Map<String, String> headers = new HashMap<>();
    /**
     * 查询参数
     */
    private  Map<String, Object> query = new HashMap<>();
    /**
     * 路径参数
     */
    private  Map<String, Object> path = new HashMap<>();
    /**
     *  body 参数
     */
    private  Map<String, Object> body = new HashMap<>();

    public HttpParam() {
    }

    public static HttpParam instance(String httpMethod,List<InParam> paramList){
        HttpParam httpParam = new HttpParam();

        if(paramList!=null){
            paramList.forEach(param -> {
                if (ObjectUtil.equals(param.getParamPosition(),1)) {
                    httpParam.headers.put(param.getParamCode(),param.getParamValue().toString());
                }else if (ObjectUtil.equals(param.getParamPosition(),2)) {
                    httpParam.query.put(param.getParamCode(),param.getParamValue());
                }else if (ObjectUtil.equals(param.getParamPosition(),3)) {
                    httpParam.path.put(param.getParamCode(),param.getParamValue());
                }else if (ObjectUtil.equals(param.getParamPosition(),4)) {
                    httpParam.body.put(param.getParamCode(),param.getParamValue());
                }else {
                    if ("GET".equals(httpMethod)) {
                        httpParam.query.put(param.getParamCode(),param.getParamValue());
                    } else {
                        httpParam.body.put(param.getParamCode(),param.getParamValue());
                    }
                }
            });
        }
        return httpParam;
    }
}
